package com.winderinfo.yikaotianxia.ui.xiangqin;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.addorder.AddOrderActivity;
import com.winderinfo.yikaotianxia.ui.my.UserBean;
import com.winderinfo.yikaotianxia.ui.shoppingcart.ShoppingCartActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DetailsActivity extends BaseActivity implements Contract.View {
    @BindView(R.id.cu_banck)
    ImageView cuBanck;
    @BindView(R.id.cu_fenxiang)
    ImageView cuFenxiang;
    @BindView(R.id.cu_image)
    ImageView cuImage;
    @BindView(R.id.cu_title)
    TextView cuTitle;
    @BindView(R.id.cu_zhuanye)
    TextView cuZhuanye;
    @BindView(R.id.cu_sum)
    TextView cuSum;
    @BindView(R.id.cu_price)
    TextView cuPrice;
    @BindView(R.id.cu_kcsum)
    TextView cuKcsum;
    @BindView(R.id.cu_type)
    LinearLayout cuType;
    @BindView(R.id.cu_tab)
    TabLayout cuTab;
    @BindView(R.id.cu_chakan)
    TextView cuChakan;
    @BindView(R.id.cu_pager)
    ViewPager cuPager;
    @BindView(R.id.cu_ce_sum)
    TextView cuCeSum;


    private Presenter presenter;
    //创建Map
    Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;

    private int sid;
    private List<String> tablList;
    private List<Fragment> fList;
    private List<KeSumBean.CoursesBean> coursesA;
    private DetailsBean.YkGoodsBean ykGoods;

    @Override
    protected int initLayoutId() {
        return R.layout.ativity_curriculumdetails;
    }

    @Override
    protected void initView() {
        //实例P
        presenter = new Presenter(this);
        spUtils = new SPUtils(this);
        //获取商品ID,Type
        Intent intent = getIntent();
        sid = intent.getIntExtra("id", 0);
        String type = intent.getStringExtra("type");
        //判断是班型还是其他
        if ("1".equals(type)) {
            cuType.setVisibility(View.VISIBLE);
        } else {
            cuType.setVisibility(View.GONE);
        }
        //TabLayout
        tablList = new ArrayList<>();
        tablList.add("课程详情");
        tablList.add("师资介绍");
        fList = new ArrayList<>();
        CourseDetailsFragment courseDetailsFragment = new CourseDetailsFragment();
        IntroductionFragment introductionFragment = new IntroductionFragment();
        fList.add(courseDetailsFragment);
        fList.add(introductionFragment);

    }

    @Override
    protected void initData() {
        //请求详情数据
        /**
         * @id 商品id
         *
         * */
        map.clear();
        map.put("id", sid);
        presenter.onPostRequest(Api.XIANGQIN, map, DetailsBean.class);
        //判断用户是否登录
        if (spUtils.getString("UserID", " ") != null) {
            //请求用户详情拿购物车数量
            /**
             * @id 用户Id
             * */
            map.clear();
            map.put("id", 1);
            presenter.onPostRequest(Api.USERXIANGQIN, map, UserBean.class);
        } else {
            cuCeSum.setVisibility(View.GONE);
        }
    }

    @Override
    public void onSuccess(Object data) {
        //详情数据
        if (data instanceof DetailsBean) {
            ykGoods = ((DetailsBean) data).getYkGoods();
            //设置数据
            Glide.with(this).load(ykGoods.getPhoto()).into(cuImage);
            cuTitle.setText(ykGoods.getTitle());
            cuSum.setText(ykGoods.getBuyNum() + "  人购买");
            cuPrice.setText("￥" + ykGoods.getPrice());
            //请求课程详情数据
            /**
             * @courses 返回商品中的courses
             * */
            map.clear();
            if (ykGoods.getCourses() != null) {
                map.put("courses", ykGoods.getCourses());
                presenter.onPostRequest(Api.ZHUYAOKECHENG, map, KeSumBean.class);
            }
            //设置适配器
            DataPagerAdapter dataPagerAdapter = new DataPagerAdapter(getSupportFragmentManager(), fList, tablList, ykGoods.getAuthorId(), ykGoods.getDetails());
            cuPager.setAdapter(dataPagerAdapter);
            cuTab.setupWithViewPager(cuPager);
        }
        //课程详情数据
        if (data instanceof KeSumBean) {
            coursesA = ((KeSumBean) data).getCourses();
            //设置课程数量
            cuKcsum.setText("共" + ((KeSumBean) data).getCourses().size() + "门课程");
        }
        //用户详情
        if (data instanceof UserBean) {
            cuCeSum.setText(((UserBean) data).getYkUser().getCartnum() + "");
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @OnClick({R.id.cu_banck, R.id.cu_chakan, R.id.cu_kefu, R.id.cu_ce, R.id.cu_jce, R.id.cu_mai})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cu_banck:
                finish();
                break;
            case R.id.cu_chakan:
                if (ykGoods.getCourses() != null) {
                    setPopup();
                }
                break;
            case R.id.cu_kefu:
                if (spUtils.getString("UserID", null) != null) {

                } else {
                    ToastUtil.showSuccess(this, "请先登录");
                }
                break;
            case R.id.cu_ce:
                if (spUtils.getString("UserID", " ") != null) {
                    openActivity(ShoppingCartActivity.class);
                } else {
                    ToastUtil.showSuccess(this, "请先登录");
                }
                break;
            case R.id.cu_jce:
                if (spUtils.getString("UserID", null) != null) {

                } else {
                    ToastUtil.showSuccess(this, "请先登录");
                }
                break;
            case R.id.cu_mai:
                if (spUtils.getString("UserID", " ") != null) {
                    openActivity(AddOrderActivity.class);
                } else {
                    ToastUtil.showSuccess(this, "请先登录");
                }
                break;
        }
    }

    private void setPopup() {
        View layout = LayoutInflater.from(this).inflate(R.layout.popup_majorcourses, null);
        ImageView ma_back = layout.findViewById(R.id.mac_back);
        TextView ma_text = layout.findViewById(R.id.mac_text);
        //循环评价课程
        StringBuilder s5 = new StringBuilder();
        for (int i = 0; i < coursesA.size(); i++) {

            if (i == coursesA.size() - 1) {
                s5.append(coursesA.get(i).getTitle());
            } else {
                s5.append(coursesA.get(i).getTitle() + "，");
            }
        }
        //设置课程
        ma_text.setText(s5);
        final PopupWindow popupWindow = new PopupWindow(layout);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置PopupWindow是否能响应外部点击事件
        popupWindow.setOutsideTouchable(false);
        popupWindow.setTouchable(true);
        popupWindow.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
        ma_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }

}
